<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Find News Thread
//-----------------------------------------------------------------

$forum_find_news = $db->getLine("SELECT id, cat FROM forum_subcat WHERE type = '2'");

if($forum_find_news != FALSE)
{
	//-----------------------------------------------------------------
	//  Navigation Stuff
	//-----------------------------------------------------------------

	$rows_per_page = $config['nav_news'];
	$numrows = $db->getInfo_countRows("forum_topics","cat = '{$forum_find_news['cat']}' AND subcat = '{$forum_find_news['id']}'");

	$lastpage = ceil($numrows/$rows_per_page);
	$p = (int)$p;

	if ($p < 1) $p = 1;
	elseif ($p > $lastpage) $p = $lastpage;

	if($numrows != FALSE) $limit = 'LIMIT ' .($p - 1) * $rows_per_page .',' .$rows_per_page;
	else $limit = NULL;

	//-----------------------------------------------------------------
	//  Get Database
	//-----------------------------------------------------------------

	$forum_topics = $db->getTable("
	
	SELECT		t.id as tid, t.name, t.cat, t.subcat, t.views,
				r.id as rid, r.topic, r.user_id, r.user_user, r.date, r.message, COUNT(r.id) AS replies,
				u.id AS uid, u.avatar, u.authorization, u.username 
				
	FROM forum_topics AS t 
	LEFT JOIN forum_replies AS r ON (r.topic = t.id)
	LEFT JOIN users AS u ON (r.user_id = u.id AND r.user_user = u.username) 
	
	WHERE t.cat = '{$forum_find_news['cat']}' AND t.subcat = '{$forum_find_news['id']}' AND r.id < '10000000000'
	GROUP BY t.id
	ORDER BY tid DESC $limit");

	//-----------------------------------------------------------------
	//  Start display
	//-----------------------------------------------------------------

	if($forum_topics != FALSE)
	{
		foreach($forum_topics as $idx=>$forum_topic)
		{
			//-----------------------------------------------------------------
			//  Count for ID + trick
			//-----------------------------------------------------------------

			$idx_count = str_replace("-", "", $idx + (($p - 1) * $rows_per_page + 1) - $numrows - 1);

			//-----------------------------------------------------------------
			//  Show some HTML
			//-----------------------------------------------------------------

			?>
				<table class="main" cellspacing="1" cellpadding="4">
				<tr class="head"><td><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr class="head"><td align="left" width="50%"><?= $forum_topic['name'] ?></td><td align="right" width="50%"><span class="inv">#<?= $idx_count ?></span></td></tr></table></td></tr>
				<tr>
				<td class="con1">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr class="empty">
				<td width="10%"><table border="0" width="79" height="80" cellspacing="0" cellpadding="0"><tr><td height='80' class='avatar'><?= find_avatar($forum_topic['avatar']) ?></td></tr></table></td>
				<td width="90%" valign="middle"><span style="font-size: 12px; font-weight: bold;"><a href="<?= $PHP_SELF ?>?n=modules/users&a=1&d=<?= $forum_topic['user_id'] ?>"><?= $forum_topic['user_user'] ?></a></span><br><?= find_authorization($forum_topic['authorization']) ?><br><br><img src="templates/<?= $template ?>/images/buttons/date.gif" border="0" alt="<?= $language['News view desc'] ?>"> <?= today_yesterday($forum_topic['date']) ?></td>
				</tr>
				<tr><td colspan="2" style="border-top: dashed 1px #d3d3d3"><table cellpadding="4" cellspacing="2" width="100%" style="text-align:justify"><tr class="empty"><td><?= parse_message($forum_topic['message']) ?></td></tr></table>
				</table>
				</td>
				</tr>
				<tr class="head"><td><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr class="empty"><td align="left" width="50%"><a href="<?= $PHP_SELF ?>?n=modules/forum&a=3&d=<?= $forum_topic['cat'] ?>&o=<?= $forum_topic['subcat'] ?>&q=<?= $forum_topic['tid'] ?>"><?= ($forum_topic['replies'] - 1); if(($forum_topic['replies'] - 1) == "1") echo " {$language['News view desc2']}"; else echo " {$language['News view desc3']}" ?></a></td><td align="right" width="50%"><?= $forum_topic['views']; if($forum_topic['views'] == "1") echo " {$language['News view desc4']}"; else echo " {$language['News view desc5']}"; ?></td></tr></table></td></tr>
				</table>
				<br>
			<?

			//-----------------------------------------------------------------
			//  Start from 1
			//-----------------------------------------------------------------

			$idx++;
		}
	}
	else
	{
		//-----------------------------------------------------------------
		//  Reset to 0
		//-----------------------------------------------------------------

		$idx = 0;

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td>{$language['News view desc6']}</td></tr></table><br>";
	}

	//-----------------------------------------------------------------
	//  Navigation Code
	//-----------------------------------------------------------------

	if($p == 1) $nav_prev = "<tr><td class='con1' align='right'>&nbsp;</td>";
	else
	{
		$prevpage = $p-1;
		$nav_prev = "<tr><td class='con1' align='left'><a href='$PHP_SELF?n=modules/news&p=$prevpage'>&laquo; {$language['News Nav previous']}</a></td>";
	}

	$nav_nr = '[ '.paginate($lastpage, $p, $PHP_SELF.'?n=modules/news').' ]';

	if($idx == $numrows) $nav_nr = NULL;

	if($p == $lastpage) $nav_next = "<td class='con1' align='right'>&nbsp;</td></tr>";
	else
	{
		$nextcode = $numrows - $idx * $p;
		if($nextcode > $idx) $how_next = $idx;
		else $how_next = $nextcode;

		$nextpage = $p+1;

		$nav_next = "<td class='con1' align='right'><a href='$PHP_SELF?n=modules/news&p=$nextpage'>{$language['News Nav next']} $how_next &raquo;</a></td></tr>";
	}

	if($idx == $numrows)
	{
		$nav_head = "class='con1'";
		$nav = NULL;
	}
	else
	{
		$nav_head = "class='head'";
		$nav = ''.$nav_prev.''.$nav_next.'';
	}

	//-----------------------------------------------------------------
	//  Navigation Display
	//-----------------------------------------------------------------

	?>
		<table class="main" cellspacing="1" cellpadding="4" width="100%">
		<col width="50%"><col width="50%">
		<tr <?= $nav_head ?>>
		<td colspan="2">
		<table border="0" cellspacing="0" cellpadding="0" width="100%">
		<col width="50%"><col width="50%">
		<tr class="empty">
		<td align="left"><?= $language['News Nav nav'] ?> <b><?= $idx ?></b> <?= $language['News Nav nav2'] ?> <b><?= $numrows ?></b></td>
		<td align="right"><?= $nav_nr ?></td>
		</tr>
		</table>
		</td>
		</tr>
		<?= $nav ?>
		</table>
	<?
}
else echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td>{$language['News view desc7']}</td></tr></table>" ?>
